from selenium.webdriver import Chrome
import time
import csv
import xlsxwriter
from selenium import webdriver
from selenium.webdriver.common.by import By

web = webdriver.Chrome(executable_path=r'd:\chromedriver.exe')
web.get('https://www.chinabond.com.cn/cb/cn/xwgg/zsxw/hgjj/list.shtml')
begin = 0
end = 1000
with open('test.csv', 'a', encoding='utf-8')as fp:
    for i in range(begin, end):
        now = []
        div = web.find_element(By.CLASS_NAME,'rightContentBox')
        a_elements_list = div.find_elements(By.TAG_NAME,'a')
        j = 0
        for a_element in a_elements_list:
            a_element.click()
            ws = web.window_handles
            web.switch_to.window(ws[1]) 
            article_div = web.find_element(By.CLASS_NAME,'allDetailBox')
            detail_div = article_div.find_element(By.CLASS_NAME,'allDeatilCon')
            title = detail_div.find_element(By.CLASS_NAME,'allDetailTitle')
            author = detail_div.find_element(By.CLASS_NAME,'allDeatilAuthor')
            publish_time = article_div.find_element(By.CLASS_NAME,'detailTime')
            list = []
            list.append(title.text)
            list.append(author.text)
            list.append(publish_time.text)
            print(list)
            now.append(list)
            web.close()
            web.switch_to.window(ws[0]) 
            j = j + 1
            if (j == 10):
                break
        web.find_element(By.CLASS_NAME, 'pageNext').click()
        fileWrite = csv.writer(fp)
        fileWrite.writerows(now)
        time.sleep(2)
web.close()
